package XynLeen.GraduationProject.Activities;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.yang.flowlayoutlibrary.FlowLayout;
import com.youth.xframe.widget.XLoadingDialog;
import com.youth.xframe.widget.XToast;
import com.zhy.http.okhttp.OkHttpUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import XynLeen.GraduationProject.Bean.HttpConstellation;
import XynLeen.GraduationProject.R;
import XynLeen.GraduationProject.Utils.JsonCallback;
import XynLeen.GraduationProject.view.TitleArrowLineItem;
import XynLeen.GraduationProject.view.BaseActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class Activity_Calendar extends BaseActivity {

    XLoadingDialog mXLoadingDialog;
    @Bind(R.id.time)
    TitleArrowLineItem mTime;
    @Bind(R.id.fl_keyword)
    FlowLayout flKeyword;
    @Bind(R.id.ib_navigation_back)
    ImageButton mIbNavigation_back;
    @Bind(R.id.ib_navigation_add)
    ImageButton mIbNavigation_add;
    @Bind(R.id.tv_navigation_label)
    TextView mTvNavigation;

    private String key = "e99e5c40b6ea265485f01e1a30643966";
    SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.bind(this);
        mXLoadingDialog = XLoadingDialog.with(this);
        mTime.setDetail(formatter1.format(new Date()));

        // 设置文字大小
        flKeyword.setTextSize(16);
        // 设置文字颜色
        flKeyword.setTextColor(Color.WHITE);
        // 设置文字背景

        // 设置文字水平margin
        flKeyword.setHorizontalSpacing(15);
        // 设置文字垂直margin
        flKeyword.setVerticalSpacing(35);

        // 设置文字水平padding
        flKeyword.setTextPaddingH(15);
        // 设置文字垂直padding
        flKeyword.setTextPaddingH(8);

        mTvNavigation.setText("良辰吉日");
        mIbNavigation_add.setVisibility(View.INVISIBLE);

        getdata();
    }

    @OnClick(R.id.ib_navigation_back)
    public void OnClick(View v){
        finish();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_calendar;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public void initView() {

    }

    @OnClick(R.id.time)
    public void time() {
        //时间选择器
        TimePickerView pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                mTime.setDetail((formatter1.format(date) + ""));
                getdata();
            }
        }).setType(new boolean[]{true, true, true, false, false, false})// 默认全部显示
                .build();
        pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show();
    }

    public void getdata() {
        mXLoadingDialog.show();
        OkHttpUtils.get()
                .url("https://v.juhe.cn/laohuangli/d")
                .addParams("key", key)
                .addParams("date", mTime.getTextViewVal())
                .build().execute(new JsonCallback<HttpConstellation>(HttpConstellation.class) {

            @Override
            public void onError(Call call, Exception e, int id) {
                mXLoadingDialog.dismiss();
            }

            @Override
            public void onResponse(HttpConstellation response, int id) {
                mXLoadingDialog.dismiss();
                if ("successed".equals(response.getReason())) {
                    //数据解析整理
                    HttpConstellation.Result result = response.getResult();
                    if (result == null) {
                        return;
                    }
                    List<String> list=new ArrayList<>();
                    list.add("阴历:" + result.getYinli());
                    list.add( "五行:" + result.getWuxing());
                    list.add("冲、煞:" + result.getChongsha());
                    list.add(  "百忌:" + result.getBaiji());
                    list.add("吉神:" + result.getJishen());
                    list.add( "宜:" + result.getYi());
                    list.add( "凶神:" + result.getXiongshen());
                    list.add("忌:" + result.getJi());
                    initDataView(list);

                } else {
                    XToast.error("接口异常");
                }
            }
        });
    }

    private void initDataView(List<String> list) {
        flKeyword.setViews(list, new FlowLayout.OnItemClickListener() {
            @Override
            public void onItemClick(String content, View view) {
                Toast.makeText(Activity_Calendar.this, content, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
